{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Calculate Digit Sum of a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: digitSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #计算字符串的数字和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个由若干数字（<code>0</code> - <code>9</code>）组成的字符串 <code>s</code> ，和一个整数。</p>\n",
    "\n",
    "<p>如果 <code>s</code> 的长度大于 <code>k</code> ，则可以执行一轮操作。在一轮操作中，需要完成以下工作：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>将 <code>s</code> <strong>拆分 </strong>成长度为 <code>k</code> 的若干 <strong>连续数字组</strong> ，使得前 <code>k</code> 个字符都分在第一组，接下来的 <code>k</code> 个字符都分在第二组，依此类推。<strong>注意</strong>，最后一个数字组的长度可以小于 <code>k</code> 。</li>\n",
    "\t<li>用表示每个数字组中所有数字之和的字符串来 <strong>替换</strong> 对应的数字组。例如，<code>\"346\"</code> 会替换为 <code>\"13\"</code> ，因为 <code>3 + 4 + 6 = 13</code> 。</li>\n",
    "\t<li><strong>合并</strong> 所有组以形成一个新字符串。如果新字符串的长度大于 <code>k</code> 则重复第一步。</li>\n",
    "</ol>\n",
    "\n",
    "<p>返回在完成所有轮操作后的 <code>s</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"11111222223\", k = 3\n",
    "<strong>输出：</strong>\"135\"\n",
    "<strong>解释：</strong>\n",
    "- 第一轮，将 s 分成：\"111\"、\"112\"、\"222\" 和 \"23\" 。\n",
    "  接着，计算每一组的数字和：1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。 \n",
    "&nbsp; 这样，s 在第一轮之后变成 \"3\" + \"4\" + \"6\" + \"5\" = \"3465\" 。\n",
    "- 第二轮，将 s 分成：\"346\" 和 \"5\" 。\n",
    "&nbsp; 接着，计算每一组的数字和：3 + 4 + 6 = 13 、5 = 5 。\n",
    "&nbsp; 这样，s 在第二轮之后变成 \"13\" + \"5\" = \"135\" 。 \n",
    "现在，s.length &lt;= k ，所以返回 \"135\" 作为答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = \"00000000\", k = 3\n",
    "<strong>输出：</strong>\"000\"\n",
    "<strong>解释：</strong>\n",
    "将 \"000\", \"000\", and \"00\".\n",
    "接着，计算每一组的数字和：0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。 \n",
    "s 变为 \"0\" + \"0\" + \"0\" = \"000\" ，其长度等于 k ，所以返回 \"000\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>2 &lt;= k &lt;= 100</code></li>\n",
    "\t<li><code>s</code> 仅由数字（<code>0</code> - <code>9</code>）组成。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [calculate-digit-sum-of-a-string](https://leetcode.cn/problems/calculate-digit-sum-of-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [calculate-digit-sum-of-a-string](https://leetcode.cn/problems/calculate-digit-sum-of-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"11111222223\"\\n3', '\"00000000\"\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            ss = ''\n",
    "            while s:\n",
    "                x, s = s[:k], s[k:]\n",
    "                c = 0\n",
    "                for _ in range(len(x)):\n",
    "                    xx, x = x[0], x[1:]\n",
    "                    c += int(xx)\n",
    "                ss += str(c)\n",
    "            s = ss\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while k < len(s):\n",
    "            t = \"\"\n",
    "            for i in range(0, len(s), k):\n",
    "                t += str(sum(map(int, s[i:k+i])))\n",
    "            s = t \n",
    "        return s\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        \n",
    "        res = 0 \n",
    "        pre_sum = 0\n",
    "\n",
    "        length = len(s)\n",
    "        level_sum = \"\"\n",
    "\n",
    "        if length <= k: return s\n",
    "\n",
    "        for i in range(length):\n",
    "            now_int_num = int(s[i])\n",
    "            pre_sum += now_int_num\n",
    "\n",
    "            if (i+1) % k == 0:\n",
    "                level_sum += str(pre_sum)\n",
    "                \n",
    "                pre_sum = 0\n",
    "            elif i == (length-1):\n",
    "                level_sum += str(pre_sum)\n",
    "   \n",
    "        # print(pre_sum, level_sum)\n",
    "\n",
    "        if len(level_sum) > k:\n",
    "            level_sum = self.digitSum(level_sum, k)\n",
    "\n",
    "        return level_sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        \n",
    "        while len(s) > k:\n",
    "            strings = []\n",
    "            i = 0\n",
    "            while (i < len(s)):\n",
    "                strings.append(s[i:min(len(s), i + k)])\n",
    "                i += k\n",
    "            s = \"\"\n",
    "            for string in strings:\n",
    "                cur = 0\n",
    "                for i in range(0, len(string)):\n",
    "                    cur += int(string[i])\n",
    "                s += str(cur)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while(len(s)>k):\n",
    "            temp = []        \n",
    "            for i in range(0,len(s),k):\n",
    "                sum1 = 0\n",
    "                for j in range(i,min(i+k,len(s))):\n",
    "                    sum1+=int(s[j])\n",
    "                temp.append(str(sum1))\n",
    "            s = \"\".join(temp)\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            tem=[]\n",
    "            n=len(s)\n",
    "\n",
    "            for i in range(0,n,k):\n",
    "                val=0\n",
    "                for j in range(i,min(i+k,n)):\n",
    "                    val+=int(s[j])\n",
    "                tem.append(str(val))\n",
    "\n",
    "            s=\"\".join(tem)\n",
    "\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        while len(s) > k :\n",
    "            pos = 0\n",
    "            temp = []\n",
    "            while pos < len(s):\n",
    "                string = list(s[pos:pos+k])\n",
    "                string = sum(list(map(int, string)))\n",
    "                temp.append(str(string))\n",
    "                pos += k\n",
    "            s = ''.join(temp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        ans = ''\n",
    "        num = 0\n",
    "        while n > k:\n",
    "            for i in range(0,n,k):\n",
    "                for j in s[i:i+k]:\n",
    "                    num += int(j)\n",
    "                ans += str(num)\n",
    "                num = 0\n",
    "            s = ans\n",
    "            print(ans)\n",
    "            ans = ''\n",
    "            n = len(s)\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            temp = []\n",
    "            n = len(s)\n",
    "            for i in range(0,n,k):\n",
    "                sum1 = 0\n",
    "                for j in range(i,min(i+k,n)):\n",
    "                    sum1+=int(s[j])\n",
    "                temp.append(str(sum1))\n",
    "            s=''.join(temp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        ptn = re.compile(r'\\d{1,' + str(k) + r'}')\n",
    "        while len(s) > k: s = ptn.sub(lambda m: str(sum(map(int, m.group()))), s)\n",
    "        return s\n",
    "\n",
    "# 作者：不造轮子\n",
    "# 链接：https://leetcode.cn/problems/calculate-digit-sum-of-a-string/solutions/1428592/by-freeyourmind-xclp/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumstring(self, string):\n",
    "        ans = 0\n",
    "        for s in string:\n",
    "            ans += int(s)\n",
    "        return str(ans)\n",
    "\n",
    "\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        if n <= k: \n",
    "            return s\n",
    "        \n",
    "        while len(s) > k:\n",
    "            ans = \"\"\n",
    "            for i in range(0, len(s), k):\n",
    "                if i + k >= len(s):\n",
    "                     ans += self.sumstring(s[i:])\n",
    "                else:\n",
    "                    ans += self.sumstring(s[i:i+k])\n",
    "            s = ans\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        while len(s) > k :\n",
    "            pos = 0\n",
    "            temp = []\n",
    "            while pos < len(s):\n",
    "                string = list(s[pos:pos+k])\n",
    "                string = sum(list(map(int, string)))\n",
    "                temp.append(str(string))\n",
    "                pos += k\n",
    "            s = ''.join(temp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while True:\n",
    "            if len(s)<=k:\n",
    "                return s\n",
    "            else:\n",
    "                r=[]                \n",
    "                for i in range(0,len(s),k):\n",
    "                    r.append(s[i:i+k])                    \n",
    "                #print(r)                \n",
    "                \n",
    "                for i in range(len(r)):                    \n",
    "                    t=[]\n",
    "                    for j in r[i]:\n",
    "                        t.append(int(j))\n",
    "                    r[i]=str(sum(t))                    \n",
    "                s=''.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        if len(s) <= k: return s\n",
    "        else: return self.digitSum(''.join(str(sum(int(x) for x in s[i : i + k])) for i in range(0, len(s), k)), k)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            temp_s = ''\n",
    "            for i in range(0, len(s), k):\n",
    "                temp_num = sum(int(i) for i in s[i:i + k])\n",
    "                temp_s += str(temp_num)\n",
    "            s = temp_s\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "          while len(s) > k:\n",
    "            m = len(s) // k\n",
    "            s1 = \"\"\n",
    "            # 切割\n",
    "            for i in range(m + 1):\n",
    "                tmp = s[k * i:k * (i + 1)]     #例如长度为11，最后一组i=3，tmp=s[9：12]也可输出的\n",
    "                sum = 0\n",
    "                # 如果后面的为空\n",
    "                if len(tmp) != 0:\n",
    "                    for j in tmp:\n",
    "                        sum = sum + int(j)\n",
    "                    s1 = s1 + str(sum)\n",
    "            s = s1\n",
    "          return s\n",
    "\n",
    "\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        if len(s) <= k: return s\n",
    "        while len(s) > k:\n",
    "            temp = list()\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i+k, n)):\n",
    "                    val += int(s[j])\n",
    "                temp.append(str(val))\n",
    "            s = \"\".join(temp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:            \n",
    "            ts, n = 0, 0\n",
    "            t = ''\n",
    "            for p, i in enumerate(s):\n",
    "                ts += int(i)\n",
    "                if (p + 1) % k == 0 or p == len(s) - 1:\n",
    "                    t += str(ts)\n",
    "                    ts = 0\n",
    "            s = t    \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def digitSum(self, s: str, k: int):\r\n",
    "        \r\n",
    "        while len(s) > k:\r\n",
    "            t= ''\r\n",
    "            for i in range(0,len(s),k):\r\n",
    "                t += str(sum(map(int, s[i:i+k])))\r\n",
    "            s = t\r\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            nums = []\n",
    "            for i in range((len(s) + k-1) // k):\n",
    "                nums.append(s[k*i:k*(i+1)])\n",
    "            for i, num in enumerate(nums):\n",
    "                n = 0\n",
    "                for c in num:\n",
    "                    n += int(c)\n",
    "                nums[i] = str(n)\n",
    "            s = \"\".join(nums)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            s=list(s)\n",
    "            tmp=''\n",
    "            n=len(s)\n",
    "            for i in range(0,n,k):\n",
    "                j=i\n",
    "                tmp+=str(sum(int(s[j]) for j in range(i,min(i+k,n))))\n",
    "            s=tmp \n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        ans = ''\n",
    "        while len(s) > k:\n",
    "            while s:\n",
    "                ans += str(sum(int(i) for i in s[:k]))\n",
    "                s = s[k:]\n",
    "            s = ans\n",
    "            ans = ''\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        n = len(s)\n",
    "\n",
    "        if n <= k:\n",
    "            return s\n",
    "            \n",
    "        while n > k:\n",
    "            m = n // k\n",
    "\n",
    "            y = n % k\n",
    "\n",
    "            if y > 0:\n",
    "                m += 1\n",
    "            text = \"\"\n",
    "            for i in range(m):\n",
    "                ss = s[k*i:k*(i+1)]\n",
    "                # print(ss)\n",
    "                ss = sum([int(x) for x in ss])\n",
    "\n",
    "                text += str(ss)\n",
    "                # print(ss)\n",
    "\n",
    "            # print(text)\n",
    "            n = len(text)\n",
    "            s = text\n",
    "\n",
    "        \n",
    "        return text\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            tmp=[int(i) for i in s]\n",
    "            _tmp=\"\"\n",
    "            for i in range(0,len(s),k):\n",
    "                _tmp+=str(sum(tmp[i:i+k]))\n",
    "            s=_tmp\n",
    "        return s\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        while n > k:\n",
    "            ss = []\n",
    "            while s:\n",
    "                try:\n",
    "                    sss = s[:k]\n",
    "                    ss.append(str(sum(int(i) for i in sss)))\n",
    "                    s = s[k:]\n",
    "                except IndexError:\n",
    "                    ss.append(str(sum(int(i) for i in s)))\n",
    "            s = \"\".join(ss)\n",
    "            n = len(s)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k :\n",
    "            temp = []\n",
    "            n = len(s)\n",
    "            for i in range(0,n,k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i+k, n)):\n",
    "                    val += int(s[j])\n",
    "                temp.append(str(val))\n",
    "            \n",
    "            s = \"\".join(temp)\n",
    "        \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while True:\n",
    "            if len(s)<=k:\n",
    "                return s\n",
    "            else:\n",
    "                r=[]                \n",
    "                for i in range(0,len(s),k):\n",
    "                    r.append(s[i:i+k])                    \n",
    "                #print(r)                \n",
    "                \n",
    "                for i in range(len(r)):                    \n",
    "                    t=[int(j) for j in r[i]]                    \n",
    "                    r[i]=str(sum(t))                    \n",
    "                s=''.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        while len(s) > k:\n",
    "            m = 0\n",
    "            arr = ''\n",
    "            for i in range(0, len(s), k):\n",
    "                temp_arr = s[i:i+k]\n",
    "                total = 0\n",
    "                for char in temp_arr:\n",
    "                    total += int(char)\n",
    "                arr += str(total)\n",
    "            s = arr\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        def digits(s,k):\n",
    "            if len(s)<=k:\n",
    "                return s\n",
    "            else:\n",
    "                n=len(s)\n",
    "                i=0\n",
    "                out=''\n",
    "                while i<n:\n",
    "                    sums=0\n",
    "                    for j in range(i,min(i+k,n)):\n",
    "                        sums+=int(s[j])\n",
    "                        i+=1\n",
    "                    out+=str(sums)\n",
    "                    \n",
    "                    \n",
    "                return digits(out,k)\n",
    "        return digits(s,k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        def couts(s):\n",
    "            ans = 0\n",
    "            for i in s:\n",
    "                ans += int(i)\n",
    "            return str(ans)\n",
    "\n",
    "        while len(s) > k:\n",
    "            t = ''\n",
    "            for i in range(0,len(s),k):\n",
    "                t += couts(s[i:i+k])\n",
    "            s = t\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while(len(s)>k):\n",
    "            l = int(len(s)/k)\n",
    "            list1 = []\n",
    "            for i in range(l):\n",
    "                list1.append(s[i*k:(i+1)*k])\n",
    "            list1.append(s[l*k:])\n",
    "            #print(list1)\n",
    "            list2 = []\n",
    "            for nums in list1:\n",
    "                if(nums == ''):\n",
    "                    break\n",
    "                nums_sum = 0\n",
    "                for i in nums:\n",
    "                    nums_sum += int(i)\n",
    "                list2.append(str(nums_sum))\n",
    "            s = \"\".join(list2)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while(len(s)>k):\n",
    "            l = int(len(s)/k)\n",
    "            list1 = []\n",
    "            for i in range(l):\n",
    "                list1.append(s[i*k:(i+1)*k])\n",
    "            list1.append(s[l*k:])\n",
    "            #print(list1)\n",
    "            \n",
    "            list2 = []\n",
    "            for nums in list1:\n",
    "                if(nums == ''):\n",
    "                    break\n",
    "                nums_sum = 0\n",
    "                for i in nums:\n",
    "                    nums_sum += int(i)\n",
    "                list2.append(str(nums_sum))\n",
    "            s = \"\".join(list2)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            new_s = ''\n",
    "            for c in range(0, len(s), k):\n",
    "                sum_son = str(sum([int(i) for i in s[c:c+k]]))\n",
    "                new_s += sum_son\n",
    "            s = new_s\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while True:\n",
    "            if len(s)<=k:\n",
    "                return s\n",
    "            else:\n",
    "                r=[]                \n",
    "                for i in range(0,len(s),k):\n",
    "                    r.append(s[i:i+k])                    \n",
    "                #print(r)                \n",
    "                for i in range(len(r)):                    \n",
    "                    t=[int(j) for j in r[i]]                    \n",
    "                    r[i]=str(sum(t))                    \n",
    "                s=''.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        while k < n:\n",
    "            res = \"\"\n",
    "            for i in range(0, n, k):\n",
    "                res += str(sum(map(int, list(s[i:i+k]))))\n",
    "            s[:] = list(res)\n",
    "            n = len(s)\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        while n > k:\n",
    "            ss = []\n",
    "            while s:\n",
    "                try:\n",
    "                    ss.append(str(sum(int(i) for i in s[:k])))\n",
    "                    s = s[k:]\n",
    "                except IndexError:\n",
    "                    ss.append(str(sum(int(i) for i in s)))\n",
    "            s = \"\".join(ss)\n",
    "            n = len(s)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "\n",
    "        pres = s \n",
    "        while len(pres) > k:\n",
    "            news = ''\n",
    "            for i in range(0, len(pres), k):\n",
    "                num = 0 \n",
    "                for j in range(min(k, len(pres) - i)):\n",
    "                    num += int(pres[i+j]) \n",
    "                news += str(num) \n",
    "            #print(news)\n",
    "            pres = news \n",
    "        return pres "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        res_l = list(s)\n",
    "        res_l = [int(num) for num in res_l]\n",
    "        while len(res_l) > k:\n",
    "            res_l = [sum(res_l[i*k:i*k+k]) for i in range(ceil(len(res_l) / k))]\n",
    "            s = \"\".join([str(num) for num in res_l])\n",
    "            res_l = list(s)\n",
    "            res_l = [int(num) for num in res_l]\n",
    "        return \"\".join([str(num) for num in res_l])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            lst = [s[i: i + k] for i in range(0, len(s), k)]\n",
    "            ss = \"\"\n",
    "            for n in lst:\n",
    "                ss += str(sum([int(j) for j in n]))\n",
    "            s = ss\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while True:\n",
    "            if len(s)<=k:\n",
    "                return s\n",
    "            else:\n",
    "                r=[]                \n",
    "                for i in range(0,len(s),k):\n",
    "                    r.append(s[i:i+k])                    \n",
    "                #print(r)                \n",
    "                \n",
    "                for i in range(len(r)):                    \n",
    "                    t=[int(j) for j in r[i]]                    \n",
    "                    r[i]=str(sum(t))                    \n",
    "                s=''.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            lst = [s[i: i + k] for i in range(0, len(s), k)]\n",
    "            s = reduce(lambda x, y: x + y, [str(sum([int(j) for j in n])) for n in lst])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        if n<=k: return s\n",
    "        def h(s):\n",
    "            return str(sum([int(i) for i in s]))\n",
    "        t = []\n",
    "        i = 1\n",
    "        while (i-1)*k<n:\n",
    "            t.append(h(s[(i-1)*k:i*k]))\n",
    "            i += 1\n",
    "        return self.digitSum(''.join(t), k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while(len(s)>k):\n",
    "            l = int(len(s)/k)\n",
    "            list1 = []\n",
    "            for i in range(l):\n",
    "                list1.append(s[i*k:(i+1)*k])\n",
    "            list1.append(s[l*k:])\n",
    "            #print(list1)\n",
    "            \n",
    "            list2 = []\n",
    "            for nums in list1:\n",
    "                if(nums == ''):\n",
    "                    break\n",
    "                nums_sum = 0\n",
    "                for i in nums:\n",
    "                    nums_sum += int(i)\n",
    "                list2.append(str(nums_sum))\n",
    "            s = \"\".join(list2)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        if n <= k:\n",
    "            return s\n",
    "        new_s = ''\n",
    "        for i in range(0,n,k):\n",
    "            new_s += str(sum([int(x) for x in list(s[i:min(i+k,n)])]))\n",
    "        return self.digitSum(new_s,k)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            n = len(s)\n",
    "            tmp = []\n",
    "            for i in range(0,n,k):\n",
    "                val = 0\n",
    "                for j in range(i,min(i+k,n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        while n >k:\n",
    "            lst1 = [sum(map(int,s[i:i+k])) for i in range(0,n,k)]\n",
    "            ss = ''.join(map(str,lst1))\n",
    "            return self.digitSum(ss,k)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            s = \"\".join([str(sum(int(j) for j in s[i:i+k])) for i in range(0, len(s), k)])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            t=''\n",
    "            x=0\n",
    "            y=0\n",
    "            for i in s:\n",
    "                y+=int(i)\n",
    "                x+=1\n",
    "                if x==k:\n",
    "                    t+=str(y)\n",
    "                    y=0\n",
    "                    x=0\n",
    "            if x!=0:\n",
    "                t+=str(y)\n",
    "                y=0\n",
    "            x=0\n",
    "            s=t\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            ans = []\n",
    "            n = len(s)\n",
    "            for i in range(0,n,k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                ans.append(str(val))\n",
    "            s = \"\".join(ans)\n",
    "        return s\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def digitSum(self, s: str, k: int) -> str:\n",
    "#         while len(s)>k:\n",
    "#             s=list(s)\n",
    "#             tmp=''\n",
    "#             n=len(s)\n",
    "#             for i in range(0,n,k):\n",
    "#                 j=i\n",
    "#                 tmp+=str(sum(int(s[j]) for j in range(i,min(i+k,n))))\n",
    "#             s=tmp \n",
    "#         return s\n",
    "\n",
    "### 官解：模拟\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            b = \"\"\n",
    "            for i in range(0, len(s), k):\n",
    "                bb = 0\n",
    "                for ss in s[i: i + k]:\n",
    "                    bb += int(ss)\n",
    "                b += str(bb)\n",
    "            s = b\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            div,mod = divmod(len(s),k)\n",
    "            print(div)\n",
    "            j = 0\n",
    "            ss = \"\"\n",
    "            m = 1 if mod else 0\n",
    "            for i in range(div+m):\n",
    "                sss = str(sum([int(k) for k in s[j:j+k]]))\n",
    "                ss += sss\n",
    "                j = j + k    \n",
    "            s = ss\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:        \n",
    "        n, t = len(s), \"\"\n",
    "  \n",
    "\n",
    "        while n > k:\n",
    "            start, end = 0, k\n",
    "            while end < n:\n",
    "                t += str(sum(map(lambda x: int(x), s[start: end])))\n",
    "                start += k\n",
    "                end += k\n",
    "\n",
    "            if end >= n:\n",
    "                t += str(sum(map(lambda x: int(x), s[start:])))\n",
    "\n",
    "\n",
    "            s = t\n",
    "            t = \"\"\n",
    "            n = len(s)\n",
    "     \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "          while len(s) > k:\n",
    "            m = len(s) // k\n",
    "            s1 = \"\"\n",
    "            # 切割\n",
    "            for i in range(m + 1):\n",
    "                tmp = s[k * i:k * (i + 1)]     #例如长度为11，最后一组i=3，tmp=s[9：12]也可输出的\n",
    "                sum = 0\n",
    "                # 如果后面的为空\n",
    "                if len(tmp) != 0:\n",
    "                    for j in tmp:\n",
    "                        sum = sum + int(j)\n",
    "                    s1 = s1 + str(sum)\n",
    "            s = s1\n",
    "          return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        if n <= k:\n",
    "            return s\n",
    "        t = [\"\"] * ((n-1) // k + 1)\n",
    "        for i in range(len(t)):\n",
    "            t[i] = str(sum(map(int, [x for x in s[i * k: (i + 1) * k]])))\n",
    "        return self.digitSum(\"\".join(t), k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = ''\n",
    "            for i in range(0, len(s), k):\n",
    "                x = s[i:i + k]\n",
    "                t = 0\n",
    "                for c in x:\n",
    "                    t += int(c)\n",
    "                tmp += str(t)\n",
    "            s = tmp\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "   def digitSum(self, s: str, k: int) -> str:\n",
    "        if len(s)<=k:\n",
    "           return s\n",
    "        res=''\n",
    "        for i in range(0,len(s),k):\n",
    "            x=s[i:i+k]\n",
    "            sum=0\n",
    "            for j in x:\n",
    "                sum+=int(j)\n",
    "            res+=str(sum)\n",
    "        return self.digitSum(res,k)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s)>k:\n",
    "            n = len(s)\n",
    "            s1 = []\n",
    "            for i in range(0,n,k):\n",
    "                if i+k<n:\n",
    "                    tmp = [int(num) for num in s[i:i+k]]\n",
    "                    s1.append(str(sum(tmp)))\n",
    "                else:\n",
    "                    tmp = [int(num) for num in s[i:]]\n",
    "                    s1.append(str(sum(tmp)))\n",
    "            s = ''.join(s1)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            s=[int(i) for i in s]\n",
    "            l = []\n",
    "            for i in range(0, len(s), k):  \n",
    "                l.append(sum(s[i:i + k]))\n",
    "            s = ''.join(map(str, l))          \n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:        \n",
    "        n, t = len(s), \"\"\n",
    "  \n",
    "\n",
    "        while n > k:\n",
    "            start, end = 0, k\n",
    "            while end < n:\n",
    "                t += str(sum(map(lambda x: int(x), s[start: end])))\n",
    "                start += k\n",
    "                end += k\n",
    "\n",
    "            if end >= n:\n",
    "                t += str(sum(map(lambda x: int(x), s[start:])))\n",
    "\n",
    "\n",
    "            s = t\n",
    "            t = \"\"\n",
    "            n = len(s)\n",
    "     \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        digits = list(s)\n",
    "        while len(digits) > k :\n",
    "            pos = 0 \n",
    "            box = []\n",
    "            while pos < len(digits) :\n",
    "                if pos + k <= len(digits) :\n",
    "                    ans = 0\n",
    "                    for x in digits[pos:pos+k] :\n",
    "                        ans += int(x)\n",
    "                    box.append(ans)\n",
    "                    pos += k\n",
    "                else :\n",
    "                    ans = 0\n",
    "                    for x in digits[pos:] :\n",
    "                        ans += int(x)\n",
    "                    box.append(ans)\n",
    "                    break\n",
    "            digits = list(\"\".join(map(str,box)))\n",
    "        return \"\".join(map(str,digits))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            \n",
    "            nex = \"\"\n",
    "            for i in range(0, len(s), k):\n",
    "                nex += str(sum(int(w) for w in s[i:i+k]))\n",
    "            s = nex\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            s=[int(i) for i in s]\n",
    "            l = []\n",
    "            for i in range(0, len(s), k):  \n",
    "                l.append(sum(s[i:i + k]))\n",
    "            s = ''.join(map(str, l))          \n",
    "\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        def jisuan(s):\n",
    "            a = [int(i) for i in list(s)]\n",
    "            return str(sum(a))\n",
    "        def chaifen(s,k):\n",
    "            p0=0\n",
    "            s1 = \"\"\n",
    "            while(True):\n",
    "                s1=s1+jisuan(s[:k])\n",
    "                if len(s[k:])>0:\n",
    "                    s = s[k:]\n",
    "                    continue\n",
    "                else:\n",
    "                    break\n",
    "            return s1\n",
    "\n",
    "        while(True):\n",
    "            if len(s)<=k:\n",
    "                break\n",
    "            else:\n",
    "                s = chaifen(s, k)\n",
    "        return (s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        digits = list(s)\n",
    "        while len(digits) > k :\n",
    "            pos = 0 \n",
    "            box = []\n",
    "            while pos < len(digits) :\n",
    "                if pos + k <= len(digits) :\n",
    "                    ans = 0\n",
    "                    for x in digits[pos:pos+k] :\n",
    "                        ans += int(x)\n",
    "                    box.append(ans)\n",
    "                    print(box)\n",
    "                    pos += k\n",
    "                else :\n",
    "                    ans = 0\n",
    "                    for x in digits[pos:] :\n",
    "                        ans += int(x)\n",
    "                    box.append(ans)\n",
    "                    print(box)\n",
    "                    break\n",
    "            digits = list(\"\".join(map(str,box)))\n",
    "        return \"\".join(map(str,digits))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            s1 = ''\n",
    "            for i in range(0, len(s), k):\n",
    "                v = sum(int(c) for c in s[i:i+k])\n",
    "                s1 = s1 + str(v)\n",
    "            s = s1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            tmp = []   # 每次操作结束的字符串对应数组\n",
    "            n = len(s)\n",
    "            for i in range(0, n, k):\n",
    "                val = 0\n",
    "                for j in range(i, min(i + k, n)):\n",
    "                    val += int(s[j])\n",
    "                tmp.append(str(val))\n",
    "            s = \"\".join(tmp)\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitSum(self, s: str, k: int) -> str:\n",
    "        while len(s) > k:\n",
    "            s = reduce(lambda x, y: x + y, [str(sum([int(j) for j in s[i: i + k]])) for i in range(0, len(s), k)])\n",
    "        return s"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
